package com.zhouxin.customview.activitys

import android.animation.AnimatorSet
import android.animation.Keyframe
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.constraintlayout.motion.widget.KeyFrames
import com.zhouxin.customview.R
import com.zhouxin.customview.databinding.ActivityAnimatorBinding
import com.zhouxin.customview.extensions.dp2px
import com.zhouxin.customview.extensions.px2dp

class AnimatorActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAnimatorBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAnimatorBinding.inflate(layoutInflater)
        setContentView(binding.root)

        /*//ViewPropertyAnimator
        binding.view.animate()
            .translationX(200.dp2px)
            .translationY(200.dp2px)
            .scaleX(1.5f)
            .scaleY(1.5f)
            .rotation(90f)
            .alpha(0.5f)
            .setDuration(2000)
            .setStartDelay(1000)
            .start()*/

/*-----------------------------------------------------------------*/

        //ObjectAnimator
        val topFlipAnimator = ObjectAnimator.ofFloat(binding.avatarView, "topFlip", -60f)
        topFlipAnimator.startDelay = 200
        topFlipAnimator.duration = 1000

        val flipRotationAnimator = ObjectAnimator.ofFloat(binding.avatarView, "flipRotation", 270f)
        flipRotationAnimator.startDelay = 200
        flipRotationAnimator.duration = 1000

        val bottomFlipAnimator = ObjectAnimator.ofFloat(binding.avatarView, "bottomFlip", 60f)
        bottomFlipAnimator.startDelay = 200
        bottomFlipAnimator.duration = 1000

        //AnimatorSet
        val animatorSet = AnimatorSet()
        animatorSet.playSequentially(topFlipAnimator, flipRotationAnimator, bottomFlipAnimator)
        animatorSet.startDelay = 500
        animatorSet.start()

/*-----------------------------------------------------------------*/

       /* //PropertyValuesHolder
        val topFlipHolder = PropertyValuesHolder.ofFloat("topFlip", -60f)
        val flipRotationHolder = PropertyValuesHolder.ofFloat("flipRotation", -270f)
        val bottomFlipHolder = PropertyValuesHolder.ofFloat("bottomFlip", 60f)
        val animatorHolder = ObjectAnimator.ofPropertyValuesHolder(
            binding.avatarView,
            topFlipHolder,
            flipRotationHolder,
            bottomFlipHolder
        )
        animatorHolder.startDelay = 1000
        animatorHolder.duration = 2000
        animatorHolder.start()*/

/*-----------------------------------------------------------------*/

        /*//KeyFrame关键帧
        val length = 200.dp2px
        val keyframe1 = Keyframe.ofFloat(0f,0f)
        val keyframe2 = Keyframe.ofFloat(0.2f,0.4f * length)
        val keyframe3 = Keyframe.ofFloat(0.8f,0.6f * length)
        val keyframe4 = Keyframe.ofFloat(1f,1f * length)
        val propertyValuesHolder = PropertyValuesHolder.ofKeyframe("translationX",keyframe1,keyframe2,keyframe3,keyframe4)
        val animator = ObjectAnimator.ofPropertyValuesHolder(binding.view,propertyValuesHolder)
        animator.startDelay = 1000
        animator.duration = 1000
        animator.start()*/
    }
}